package com.kjj;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

public class KjjActivity extends Activity {

    final static int REQUEST_CAPTURE = 0;
    final static int REQUEST_CROP = 1;
    final static int REQUEST_BROWSER = 2;

    final static int LOGIN_VIEW = 0;
    final static int POST_VIEW = 1;
    final static int OPTION_VIEW = 2;

    private ProductList productList;
    private LoginView loginView;
    private PostView postView;
    private OptionView optionView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	// TelephonyManager tm = (TelephonyManager)
	// getSystemService(TELEPHONY_SERVICE);
	// System.out.println("deviceId:"+tm.getDeviceId());
	// String android_id = Secure.getString(getContentResolver(),
	// Secure.ANDROID_ID);
	// System.out.println("android_id:"+android_id);
	productList = new ProductList(this);
	productList.initList();
	loginView = new LoginView(this, productList);
	postView = new PostView(this, productList);
	optionView = new OptionView(this, productList);
	switchView(LOGIN_VIEW);
    }

    public void switchView(int view) {
	switch (view) {
	case LOGIN_VIEW:
	    loginView.startView();
	    break;
	case POST_VIEW:
	    postView.startView();
	    break;
	case OPTION_VIEW:
	    optionView.startView();
	    break;
	}
    }

    public void requestActivity(int request, Uri uri) {
	Intent intent = null;
	switch (request) {
	case REQUEST_CAPTURE:
	    intent = new Intent("android.media.action.IMAGE_CAPTURE");
	    startActivityForResult(intent, REQUEST_CAPTURE);
	    break;
	case REQUEST_CROP:
	    try {
		intent = new Intent("com.android.camera.action.CROP");
		intent.setClassName("com.android.gallery",
			"com.android.camera.CropImage");
		intent.setData(uri);
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", Const.PICTURE_SIZE);
		intent.putExtra("outputY", Const.PICTURE_SIZE);
		intent.putExtra("noFaceDetection", true);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, REQUEST_CROP);
	    } catch (Exception e) {
		intent = new Intent("com.android.camera.action.CROP");
		intent.setData(uri);
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", Const.PICTURE_SIZE);
		intent.putExtra("outputY", Const.PICTURE_SIZE);
		intent.putExtra("noFaceDetection", true);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, REQUEST_CROP);
	    }
	    break;
	case REQUEST_BROWSER:
	    intent = new Intent(Intent.ACTION_VIEW);
	    intent.setData(uri);
	    startActivity(intent);
	    break;
	}
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	if (resultCode != Activity.RESULT_OK) {
	    return;
	}
	switch (requestCode) {
	case REQUEST_CAPTURE:
	    requestActivity(REQUEST_CROP, data.getData());
	    break;
	case REQUEST_CROP:
	    Bundle extras = data.getExtras();
	    if (extras != null) {
		Bitmap photo = extras.getParcelable("data");
		postView.addImage(photo);
	    }
	    break;
	}
    }
}